package com.vanke.kotlin_mvp.statistics

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.vanke.kotlin_mvp.R
import kotlinx.android.synthetic.main.statistics_fragment.*

/**
 * @name Kotlin_MVP
 * @package name：com.vanke.kotlin_mvp.statistics
 * @class describe
 * @anthor shiyuchuan
 * @time 2017/8/22 14:29
 */
class StatisticsFragment : Fragment(), StatisticsContract.View {

    override var presenter: StatisticsContract.Presenter? = null

    override val isActive: Boolean
        get() = isAdded

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val root = inflater.inflate(R.layout.statistics_fragment, container, false)
        return root
    }

    override fun onResume() {
        super.onResume()
        presenter?.start()
    }

    override fun setProgressIndicator(active: Boolean) {
        if (active) {
            statistics.text = "LOADING"
        } else {
            statistics.text = ""
        }
    }

    override fun showStatistics(incomplete: Int, complete: Int) {
        if (complete == 0 && incomplete == 0) {
            statistics.text = "You have no tasks"

        } else {
            val displayString = "Active Tasks: $incomplete\n" +
                    "Completed Tasks: $complete"
            statistics.text = displayString
        }
    }

    override fun showLoadingStatisticsError() {
        statistics.text = "Error loading statistics"
    }

    companion object {
        fun newInstance(): StatisticsFragment {
            return StatisticsFragment()
        }
    }
}